/**
 * OSS is a Open Service Container which gives the leverage of building the Desktop and Web
 * Application. * Copyright (C) 2008-2009  Ponraj Sutanthiramani
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Or5e Open Source Community
 * 105, S.S Paradise Apartment, Munnireddy Layout, Horamavu, Banaswadi, Bangalore, India.
 */
package org.or5e.core;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.or5e.api.bm.BulkMailConfig;

/**
 * This is the Utility Class which has all the Utility Operation which will be carried out by this API. The primary objective of this class is the provide the resuability across the API.
 * @author Ponraj suthanthiramani
 */
public class BaseUtils extends BaseObject {

    private static BaseUtils _utility = new BaseUtils();
    public static String emailValidationPattern = ".+@.+\\.[a-z]+";
    private BaseUtils() {
    }

    /**
     * Factory method, using which any Object of API gain access to this Utility methods.
     * @return Object
     */
    public static BaseUtils getUtility() {
        return _utility;
    }

    /**
     * Utility Method used to verify the string is null and empty
     * @param _isEmpty
     * @return Boolean
     */
    public final Boolean isEmptyString(String _isEmpty) {
        if (_isEmpty == null || _isEmpty.trim().equals("")) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /**
     * Utility Method used to verify is the String is not null nor empty
     * @param _isNotEmpty
     * @return Boolean
     */
    public final Boolean isNotEmptyString(String _isNotEmpty) {
        return !isEmptyString(_isNotEmpty);
    }

    /**
     * Utility Method use to verify is the array is null and empty
     * @param <T>
     * @param _array
     * @return Boolean
     */
    public final <T> Boolean isEmptyArray(T[] _array) {
        if (_array != null && _array.length > 0) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    /**
     * Utility Method used to verify is the array is not null nor empty
     * @param <T>
     * @param _array
     * @return Boolean
     */
    public final <T> Boolean isNotEmptyArray(T[] _array) {
        return !isEmptyArray(_array);
    }

    /**
     * Utility Method used to convert the Date object into String with the specified format (MMDDYY) or anything what the API likes
     * @param _dt
     * @param _format
     * @return Boolean
     */
    public final String getDateAsString(Date _dt, String _format) {
        SimpleDateFormat _dtFormat = new SimpleDateFormat(_format);
        return _dtFormat.format(_dt);
    }

    /**
     * Utility Method used to convert the Calendar object into String with the specified format (MMDDYY) or anything what the API likes
     * @param _cal
     * @param _format
     * @return String
     */
    public final String getCalAsString(Calendar _cal, String _format) {
        SimpleDateFormat _dtFormat = new SimpleDateFormat(_format);
        return _dtFormat.format(_cal.getTime());
    }

    /**
     * Utility Method used to Create a Calendar object providing a string date and the format you specify the date format
     * @param _date
     * @param _format
     * @return Calendar
     */
    public final Calendar getStringAsCal(String _date, String _format) throws ParseException {
        Calendar _cal = Calendar.getInstance();
        if (_date != null && _date.length() > 0) {
            _cal = new GregorianCalendar();
            SimpleDateFormat dateFormat = new SimpleDateFormat(_format);
            _cal.setTime(dateFormat.parse(_date));
        }
        return _cal;
    }

    /**
     * Utility Method used to verify the email address, we use the regular expression to find this ".+@.+\\.[a-z]+"
     * @param email
     * @return boolean
     */
    public boolean isValidEmail(String email) {
        Pattern p = Pattern.compile(emailValidationPattern);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    	/**
	 * Utility Method, which is specifically designed for the bulk mail module, which needs to validate the invalid email and and put it into the good and bad file and assign the file names in the Bulk mail configuration object
	 * @param config
	 * @return String
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
    public String[] validateEmail(BulkMailConfig config) throws FileNotFoundException, IOException {
        File mailFile = config.getDataFile();
        String absPath = mailFile.getAbsolutePath();
        BufferedReader mailReader = new BufferedReader(new FileReader(mailFile));
        String email = mailReader.readLine();
        String[] fileList = new String[]{absPath + ".Good", absPath + ".Bad"};
        BufferedWriter goodFileWriter = new BufferedWriter(new FileWriter(fileList[0]));
        BufferedWriter badFileWriter = new BufferedWriter(new FileWriter(fileList[1]));
        while (email != null) {
            if (isValidEmail(email)) {
                goodFileWriter.write(email + "\n");
            } else {
                badFileWriter.write(email + "\n");
            }
            email = mailReader.readLine();
        }
        badFileWriter.close();
        goodFileWriter.close();
        mailReader.close();
        return fileList;

    }

    @Override
    public String getName() {
        return "BaseUtils";
    }
}
